package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.BaseActivity;

import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/11/9.
 * * 找回密码
 */

public class BackPwdActivity extends BaseActivity implements View.OnClickListener {
    private EditText back_usernichen;
    private EditText backpwd_code_edit;
    private Button backpwd_code_btn;
    private Button btn_backpwd;
    private Resources resources;
    private TimeCount time;
    private String back_usernichenssting = "", backpwd_code_editstring = "", getcode = "";
    private String TAG = "BackPwdActivity";
    private EditText back_usernichen02;
    private EditText back_usernichen03;
    private String pwd01 = "", pwd02 = "", phone = "";
    private ImageView backpwd_img;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backpwd);
        initViews();
    }

    public void initViews() {
        back_usernichen = (EditText) findViewById(R.id.back_usernichen);
        backpwd_code_edit = (EditText) findViewById(R.id.backpwd_code_edit);
        backpwd_code_btn = (Button) findViewById(R.id.backpwd_code_btn);
        btn_backpwd = (Button) findViewById(R.id.btn_backpwd);
        back_usernichen02 = (EditText) findViewById(R.id.back_usernichen02);
        back_usernichen03 = (EditText) findViewById(R.id.back_usernichen03);
        backpwd_img = (ImageView) findViewById(R.id.backpwd_img);
        backpwd_img.setOnClickListener(this);
        backpwd_code_btn.setOnClickListener(this);
        btn_backpwd.setOnClickListener(this);
        resources = getResources();
        // 短信验证倒计时
        time = new TimeCount(60000, 1000);//构造CountDownTimer对象
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backpwd_img:
                finish();
                break;
            case R.id.backpwd_code_btn:
                sendcode();
                break;
            case R.id.btn_backpwd:
                back_pwd();
                break;
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            backpwd_code_btn.setTextColor(resources.getColor(R.color.primary_text));
            backpwd_code_btn.setText("重新获取");
            backpwd_code_btn.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            backpwd_code_btn.setClickable(false);
            backpwd_code_btn.setTextColor(resources.getColor(R.color.gray));
            backpwd_code_btn.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    private void back_pwd() {
        back_usernichenssting = back_usernichen.getText().toString().trim();
        backpwd_code_editstring = backpwd_code_edit.getText().toString().trim();
        pwd01 = back_usernichen02.getText().toString().trim();
        pwd02 = back_usernichen03.getText().toString().trim();
        LogUtil.i(TAG, "phone" + phone + "password" + pwd01);
        if (TextUtils.isEmpty(back_usernichenssting)) {
            ToastUtils.ShortToast("请输入电话号码！");
            return;
        }
        if (TextUtils.isEmpty(backpwd_code_editstring)) {
            ToastUtils.ShortToast("请输入验证码！");
            return;
        }
        if (TextUtils.isEmpty(pwd01)) {
            ToastUtils.ShortToast("密码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(pwd02)) {
            ToastUtils.ShortToast("密码不能为空！");
            return;
        }
        if (!pwd01.equals(pwd02)) {
            ToastUtils.ShortToast("2次密码不一致！请重新输入密码!");
            return;
        }
        OkHttpUtils.post().url(BaseApi.BACKPWD)
                .addParams("telephone", back_usernichenssting)
                .addParams("code", backpwd_code_editstring)
                .addParams("newPass", pwd01)
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        ToastUtils.ShortToast("密码重置成功");
                        finish();
                    }else {
                        ToastUtils.ShortToast(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void sendcode() {
        back_usernichenssting = back_usernichen.getText().toString().trim();
        if (TextUtils.isEmpty(back_usernichenssting)) {
            ToastUtils.ShortToast("电话号码不能为空！");
            return;
        }
        String url = BaseApi.SEND_CODE;
        OkHttpUtils.post()
                .url(url)
                .addParams("telephone", back_usernichenssting)
                .addParams("category", "2")
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.i(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        getcode = jsonObject.getString("data");
                        ToastUtils.showLong(BackPwdActivity.this, getcode);
                    }else {
                        ToastUtils.ShortToast(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        time.start();
        //通过短信接口
    }
}
